from kafka import KafkaProducer

producer = KafkaProducer(bootstrap_servers='master:9092')

with open('../data/students.json', mode='r', encoding='utf-8') as f:
    lines = [line.strip() for line in f.readlines()]

# kafka-topics.sh --bootstrap-server master:9092 --create --topic students_partition --partitions 3
# 3、循环将数据写入kafka

for line in lines:
    producer.send('students_partition', line.encode('utf-8'))
    producer.flush()

producer.close()